<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- 第一排 -->
      <el-row :gutter="20">
<!--        <el-col  :xs="24" :sm="12" :lg="6">-->
<!--          <el-form-item label="ID" prop="username">-->
<!--            <el-input-->
<!--              v-model="queryParams.username"-->
<!--              placeholder="模糊查询"-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

        <el-col  :xs="24" :sm="12" :lg="6">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="模糊查询"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col  :xs="24" :sm="12" :lg="6">
          <el-form-item label="手机号" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="模糊查询"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col  :xs="24" :sm="12" :lg="6">
          <el-form-item label="用户昵称" prop="username">
            <el-input
              v-model="queryParams.username"
              placeholder="模糊查询"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>

<!--      &lt;!&ndash; 第二排 &ndash;&gt;-->
<!--      <el-row :gutter="20">-->
<!--        <el-col :xs="24" :sm="12" :lg="6">-->
<!--          <el-form-item label="变更余额" prop="money">-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              placeholder=""-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--            <div style="margin: 0px 4px;">至</div>-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :xs="24" :sm="12" :lg="6">-->
<!--          <el-form-item label="变更前" prop="money">-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              placeholder=""-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--            <div style="margin: 0px 4px;">至</div>-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :xs="24" :sm="12" :lg="6">-->
<!--          <el-form-item label="变更后" prop="money">-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              placeholder=""-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--            <div style="margin: 0px 4px;">至</div>-->
<!--            <el-input-->
<!--              v-model="queryParams.money"-->
<!--              style="max-width: 40%;"-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col  :xs="24" :sm="12" :lg="6">-->
<!--          <el-form-item label="备注" prop="memo">-->
<!--            <el-input-->
<!--              v-model="queryParams.memo"-->
<!--              placeholder="模糊查询"-->
<!--              clearable-->
<!--              @keyup.enter="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->

      <!-- 第三排 -->
      <el-row :gutter="20">
<!--        <el-col  :xs="24" :sm="12" :lg="12">-->
<!--          <el-form-item label="ID" prop="username">-->
<!--            <el-date-picker-->
<!--              v-model="value1"-->
<!--              type="datetimerange"-->
<!--              range-separator="To"-->
<!--              start-placeholder="Start date"-->
<!--              end-placeholder="End date"-->
<!--              style="flex:1"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

        <el-col  :xs="24" :sm="12" :lg="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['userInfo:moneylog:add']"
        >添加</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['userInfo:moneylog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userInfo:moneylog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['userInfo:moneylog:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList">
        1
      </right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneylogList" @selection-change="handleSelectionChange" @sort-change="changeTableSort">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员ID" align="center" sortable="custom" v-if="columns[0].visible" prop="userId" />
      <el-table-column label="姓名" align="center" sortable="custom" v-if="columns[1].visible" prop="name" />
      <el-table-column label="手机号" align="center" sortable="custom" v-if="columns[2].visible" prop="mobile" />
      <el-table-column label="昵称" align="center" sortable="custom" v-if="columns[3].visible" prop="username" />
      <el-table-column label="变更余额" align="center" sortable="custom" v-if="columns[4].visible" prop="money" />
      <el-table-column label="变更前余额" align="center" sortable="custom" v-if="columns[5].visible" prop="before" />
      <el-table-column label="变更后余额" align="center" sortable="custom" v-if="columns[6].visible" prop="after" />
      <el-table-column label="备注" align="center" sortable="custom" v-if="columns[7].visible" prop="memo" />
      <el-table-column label="创建时间" align="center" sortable="custom" v-if="columns[8].visible" prop="createtime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['userInfo:moneylog:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['userInfo:moneylog:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员余额变动对话框 -->
    <el-dialog :title="title" v-model="open" width="50vw" append-to-body :close-on-click-modal="false">
      <el-form ref="moneylogRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户ID">
          <inputsearch  :changeValue="changeValue" :inputName="'userId'" :inputValue="form.userId" :list="userList" />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.name" disabled />
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="form.username" disabled />
        </el-form-item>
        <el-form-item label="当前余额">
          <el-input v-model="form.before" disabled />
        </el-form-item>
        <el-form-item label="变更数额" prop="money">
          <el-input v-model="form.money" placeholder="请输入余额变更数额" type="number" @input="changeMoneyValue" />
        </el-form-item>
        <el-form-item label="变更后余额">
          <el-input v-model="form.after" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" :rows="2" type="textarea" placeholder="请输入变更备注/说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">保 存</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Moneylog">
import { listMoneylog, getMoneylog, delMoneylog, addMoneylog, updateMoneylog } from "@/api/userInfo/moneylog";
import { listsUser } from "@/api/userInfo/group";
import inputsearch from "@/components/inputsearch";


const { proxy } = getCurrentInstance();

const columns = ref([
  { key: 0, label: `会员ID`,prop: 'userId', visible: true },
  { key: 1, label: `姓名`,prop: 'name', visible: true },
  { key: 2, label: `手机号`,prop: 'mobile', visible: true },
  { key: 3, label: `昵称`,prop: 'username', visible: true },
  { key: 4, label: `变更余额`,prop: 'money', visible: true },
  { key: 5, label: `变更前余额`,prop: 'before', visible: true },
  { key: 6, label: `变更后余额`,prop: 'after', visible: true },
  { key: 7, label: `备注`,prop: 'memo', visible: true },
  { key: 8, label: `创建时间`,prop: 'createtime', visible: true }
]);
const moneylogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const userList = ref([]);                         // 用户列表
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    money: null,
    before: null,
    after: null,
    memo: null,
    createtime: null,
    orderByColumn: null,
    isAsc: null
  },
  rules: {
    money: [
      { required: true, message: "变更余额不能为空", trigger: "blur" }
    ],
    memo: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员余额变动列表 */
function getList() {
  loading.value = true;
  listMoneylog(queryParams.value).then(response => {
    moneylogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询用户列表 */
function getUserList() {
  listsUser({
    pageNum: 1,
    pageSize: 100
  }).then(response => {
    userList.value = response.rows.map(e => {
      e.value = e.username+(e.name ? '('+e.name+')' : '')
      return e
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    money: null,
    before: null,
    after: null,
    memo: null,
    createtime: null
  };
  proxy.resetForm("moneylogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.orderByColumn = null
  queryParams.value.isAsc = null
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMoneylog(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会员余额变动";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["moneylogRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMoneylog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMoneylog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员余额变动编号为"' + _ids + '"的数据项？').then(function() {
    return delMoneylog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('userInfo/moneylog/export', {
    ...queryParams.value
  }, `moneylog_${new Date().getTime()}.xlsx`)
}

// 改变值
const changeValue = (request) => {
  form.value = Object.assign({},{
    ...request,
    after: request.before
  })
}

function changeMoneyValue(e){
  form.value.after = Number(form.value.before) + Number(e)
}

// 改变排序
function changeTableSort(e){
  if(e.order){
    queryParams.value.orderByColumn = e.prop
    queryParams.value.isAsc = e.order == 'descending' ? 'desc' : 'asc'
  }else{
    queryParams.value.orderByColumn = null
    queryParams.value.isAsc = null
  }
  handleQuery()
}

getList();
getUserList()
</script>
